package com.michaelwolf.mytool.wcfcompmanger.wcfanalyzer;

import org.w3c.dom.Node;

import com.sun.org.apache.xerces.internal.dom.DeferredAttrImpl;
import com.sun.org.apache.xerces.internal.dom.DeferredElementImpl;

public class NodeHandler {
	
	public static Node getOwnerCompNode(Node node) {
		if(node instanceof DeferredElementImpl && isComponentNode(node)){
			return node;
		}
		DeferredAttrImpl attr = (DeferredAttrImpl)node;
		
		Node parent = attr.getOwnerElement(); 
		if(parent==null){
			System.out.println("can't find owenr comp tag for "+ attr.getName());
			return null;
		}
		
		while(!isComponentNode(parent)){
			parent = parent.getParentNode();
			String parentNodeName = parent.getNodeName();
			if(parentNodeName.equals("module")|| parentNodeName.equals("types")){
				return null;
			}
		}
		return parent;
	}
	
	public static boolean isComponentNode(Node	node) {
		String nodeName = node.getNodeName();
		
		boolean isComponentNodeName = nodeName.equals("query") || nodeName.equals("composite-view") ||
				nodeName.equals("script-function") ||nodeName.equals("task") ||
				nodeName.equals("icon") || nodeName.equals("view") ||
				nodeName.equals("association") ||nodeName.equals("renderer") ||
				nodeName.equals("script-map-function") ||nodeName.equals("map-function")
				||nodeName.equals("type")|| nodeName.equals("type-mapping");
		
		Node parent = node.getParentNode();
		String parentNodeName = parent.getNodeName();
		
		
		
		return (parentNodeName.equals("module")|| parentNodeName.equals("types"))&& isComponentNodeName;
	}
}
